package com.example.demo.service.impl;

import com.example.demo.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: yyc
 * @time: 2021/11/24 15:29
 */
@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Async
    @Override
    public void sendEmail(String to, String text) {
        this.sendEmail("yunshan_caigou2021@163.com",to,"test",text);
    }

    @Override
    public void sendEmail(String from,String to,String subject,String text) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 发件人
        simpleMailMessage.setFrom(from);
        // 收件人
        simpleMailMessage.setTo(to);
        // 邮件主题
        simpleMailMessage.setSubject(subject);
        // 邮件内容
        simpleMailMessage.setText(text);
        log.info("开始发送");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        javaMailSender.send(simpleMailMessage);
        log.info("邮件已发送");
    }

    @Override
    public void sendEmail(String to, String subject, String text) {
        this.sendEmail("yunshan_caigou2021@163.com",to,subject,text);
    }
}
